/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weiyi.chat.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.weiyi.chat.R;
import com.weiyi.chat.api.IRegister;
import com.weiyi.chat.common.base.BaseActivity;
import com.weiyi.chat.presenter.RegisterPresenter;
import com.weiyi.chat.utils.PreferenceUtil;
import com.weiyi.chat.utils.ToastUtil;

import butterknife.BindView;

/**
 * register screen
 */
public class RegisterActivity extends BaseActivity implements IRegister.Views {

    @BindView(R.id.username)
    EditText mUsername;
    @BindView(R.id.password)
    EditText mPassword;
    @BindView(R.id.confirm_password)
    EditText mConfirmPassword;
    @BindView(R.id.id_number)
    EditText mIdNumber;
    @BindView(R.id.real_name)
    EditText mRealName;
    private RegisterPresenter mRegisterPresenter;


    @Override
    protected int getLayoutResId() {
        return R.layout.em_activity_register;
    }

    @Override
    protected void initView() {
        mRegisterPresenter = new RegisterPresenter(this);

    }

    @Override
    protected void initData() {}

    public void register(View view) {
        final String username = mUsername.getText().toString().trim();
        final String pwd = mPassword.getText().toString().trim();
        String confirm_pwd = mConfirmPassword.getText().toString().trim();
        String real_name = mRealName.getText().toString().trim();
        String id_number = mIdNumber.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, getResources().getString(R.string.User_name_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mUsername.requestFocus();
            return;
        } else if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, getResources().getString(R.string.Password_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mPassword.requestFocus();
            return;
        } else if (TextUtils.isEmpty(confirm_pwd)) {
            Toast.makeText(this, getResources().getString(R.string.Confirm_password_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mConfirmPassword.requestFocus();
            return;
        } else if (!pwd.equals(confirm_pwd)) {
            Toast.makeText(this, getResources().getString(R.string.Two_input_password), Toast.LENGTH_SHORT).show();
            return;
        }else if (TextUtils.isEmpty(real_name)){
            Toast.makeText(this, getResources().getString(R.string.real_name_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mRealName.requestFocus();
            return;
        }else if (TextUtils.isEmpty(id_number)){
            Toast.makeText(this, getResources().getString(R.string.id_number_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mIdNumber.requestFocus();
            return;
        }
        mRegisterPresenter.registerAccount(username, pwd, real_name, id_number);
    }

    public void back(View view) {
        finish();
    }

    @Override
    public void onRegisterSucess() {
        PreferenceUtil.setEdit("register_account", mUsername.getText().toString().trim(), mAct);
        finish();
    }

    @Override
    public void showError(String msg) {
        ToastUtil.showTextToast(mAct, msg);
    }
}
